package com.wannengbang.cloudleader.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.AgentListBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.homepage.AgentPerformanceActivity;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.SPManager;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 合伙人列表
 */
public class AgentListAdapter extends RecyclerView.Adapter<AgentListAdapter.ViewHolder> {

    private List<AgentListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private UserInfoBean userInfoBean;

    public AgentListAdapter(List<AgentListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_agent_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AgentListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        if ((dataBean.getDepth() - userInfoBean.getData().getDepth()) == 1) {
            holder.tvName.setText(dataBean.getName());
            holder.ivZhiShu.setVisibility(View.VISIBLE);
        } else {
            holder.ivZhiShu.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(dataBean.getName())) {
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < dataBean.getName().length() - 1; i++) {
                    buffer.append("*");
                }
                String name = dataBean.getName().substring(0, 1) + buffer.toString();
                holder.tvName.setText(name);
            } else {
                holder.tvName.setText("");
            }
        }
        if ((dataBean.getDepth() - userInfoBean.getData().getDepth()) == 1) {
            holder.tvMobile.setText("手机号：" + dataBean.getMobile());
        } else {
            if (!TextUtils.isEmpty(dataBean.getMobile()) && dataBean.getMobile().length() == 11) {
                String mobile = dataBean.getMobile().substring(0, 3) + "****" + dataBean.getMobile().substring(7, 11);
                holder.tvMobile.setText("手机号：" + mobile);
            } else {
                holder.tvMobile.setText("手机号：" + dataBean.getMobile());
            }
        }


        holder.tvCreateTime.setText("创建时间：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));

        holder.itemView.setOnClickListener(v -> {
            String name = "";
            if ((dataBean.getDepth() - userInfoBean.getData().getDepth()) == 1) {
                name = dataBean.getName();
            } else {
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < dataBean.getName().length() - 1; i++) {
                    buffer.append("*");
                }
                name = dataBean.getName().substring(0, 1) + buffer.toString();
            }
            Intent intent = new Intent(context, AgentPerformanceActivity.class);
            intent.putExtra("name", name);
            intent.putExtra("agent_id", dataBean.getId() + "");
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_mobile)
        TextView tvMobile;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.iv_zhi_shu)
        ImageView ivZhiShu;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
